<?php

// +----------------------------------------------------------------------
// | 权限组控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\pos\admin;

use app\event\ucenter\User as UserEvent;
use app\model\ucenter\AuthGroupAccess; 
use app\model\pos\PosVip as PosVipModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use app\model\ucenter\AuthGroup as AuthGroupModel;
use app\model\admin\Menu as MenuModel;
use app\event\ucenter\AuthGroup as AuthGroupEvent;
use app\controller\admin\Common;
use app\model\ucenter\User as UserModel;

class PosVip extends Common
{
    protected function initialize()
    {
        parent::initialize(); 
    }

    public function _filter()
    {
        $where[] = ['status', '>', -2];
        $where[] = ['is_show', '=', 1];
        return $where;
    }

    public function index()
    {
        $where = $this->_filter();
        // 显示页面
        $builder = new Table();
        $builder->model(PosVipModel::class)
            ->where($where)
            ->order('id DESC')
//                ->suggest('高等级可以对低等级用户进行操作或者授权')
            ->searchText('title', '创业包名称：')
            ->buttonUpdate('update')
            ->keyId()
            ->keyTitle('创业包名称')
            ->keyText('pos_num','包含数量')
            ->keyDecimal('price', '价格')
            ->keyBool('is_level','VIP权益包')
//            ->keyText('commission','交易返佣')
//				->keyText('description', '描述')
				->keyUpdateTime()
//            ->keyStatus()
            ->quickUpdate(['title','description'])
				->actionUpdate()
            ->actionDelete();
//            ->actionForbid('forbid?id={$id}', '禁用', function ($data) {
//                return 1 == $data['status'] && 1 < $data['id'];
//            });
        return $builder->fetch();
    }


    public function update()
    {
        if ($this->request->isPost()) {
            try {

                if ($this->request->param('id'))
                {
                    $group = PosVipModel::where('id', $this->request->param('id'))->find();
                    if (!$group) {
                        throw new Exception('权限不存在');
                    }
                    $group['cover_id'] = $this->request->param('cover_id');
                    $group['image_id'] = $this->request->param('image_id');
                    $group['body'] = $this->request->param('body');
                    $group['is_level'] = $this->request->param('is_level');
                    $group['title'] = $this->request->param('title');
                    $group['pos_num'] = $this->request->param('pos_num');
                    $group['description'] = $this->request->param('description');
                    $group['price'] = $this->request->param('price');
                    $group['level'] = $this->request->param('level');
                    $data['message'] = '保存成功';
                    $group->save();
                }else{
                    // 新增
                    $group['module'] = 'im';
                    $group['price'] = $this->request->param('price');
                    $group['title'] = $this->request->param('title');
                    $group['cover_id'] = $this->request->param('cover_id');
                    $group['image_id'] = $this->request->param('image_id');
                    $group['is_level'] = $this->request->param('is_level');
                    $group['pos_num'] = $this->request->param('pos_num');
                    $group['level'] = $this->request->param('level');
                    $group['body'] = $this->request->param('body');
                    $group['description'] = $this->request->param('description');

                    PosVipModel::create($group);
                    $data['message'] = '新增成功';
                }

                $data['code'] = 0;
            } catch (Exception $e) {
                $data['code'] = 1;
                $data['message'] = $e->getMessage();
            }
            if (0 === $data['code']) {
                $this->_returnSuccess($data['message']);
            } else {
                $this->_returnError($data['message'], false);
            }
        } else {
            // 同步节点与菜单
            if ($this->request->param('id')) {
                $group_id = $this->request->param('id');
                $data = PosVipModel::where('id', $group_id)->find();
                if (!$data) {
                    $this->_returnError('标签不存在');
                }
            } else {
                $data = [];
            }
                $builder = new Edit();
                $builder->title('POS创业包管理')

                    ->keyTextInline('title', '创业包名称', '')
                    ->keyDecimal('price', '价格', '')
                    ->keyNumber('pos_num','包含数量')
                    ->keyNumber('level','权益等级')
                    ->keyBool('is_level','VIP权益包')
                    ->keyImageModel('cover_id','封面')
                    ->keyImageModel('image_id','内图')
                    ->keyTextArea('description', '简要信息')
                    ->keyEditor('body','详情')
//                    ->setTrigger('is_silence',1,'pkg_url')
//                    ->setTrigger('is_silence',0,'wgt_url')
//                        ->explain('1台权益包机器')
                    ->data($data)
                    ->buttonSubmit();
                return $builder->fetch();
        }
    }


}
